
import gamestate
import pygame
import randomplayer
import humanplayer
import guiutil


def draw_box(name, is_visible, nr_black, nr_white, type = "", draw_score = False):
    """
    Draws box of black and white stones.
    """
    line_width = 3
    (width, height) = (200, 200)
    surface = pygame.Surface((width, height * 1.6))
    surface.fill([255, 255, 255])

    #draw lower cone:
    pygame.draw.ellipse(surface, [0, 0, 0], [0, 0, width, height])
    pygame.draw.rect(surface, [255, 255, 255], [0, 0, width, height * 0.85])

    #draw upper cone:
    if not is_visible:
        pygame.draw.ellipse(surface, [0, 0, 0], [0, 0, width, height])
        pygame.draw.rect(surface, [255, 255, 255], [0, 0, width, height * 0.25])
    else:

        lbl_black = guiutil.draw_label("black", 70, 30, True, 0.2)
        lbl_white = guiutil.draw_label("white", 70, 30, False, 0.2)
        surface.blit(lbl_black, (25, 130))
        surface.blit(lbl_white, (105, 130))

        lbl_black_count = guiutil.draw_label(str(nr_black), 70, 50, False, 0.2, False)
        lbl_white_count = guiutil.draw_label(str(nr_white), 70, 50, False, 0.2, False)
        surface.blit(lbl_black_count, (25, 70))
        surface.blit(lbl_white_count, (105, 70))

    pygame.draw.line(surface, [0, 0, 0], [width / 2, 0], [width / 2, height], line_width)
    pygame.draw.ellipse(surface, [0, 0, 0], [0, 0, width, height], line_width)
    pygame.draw.rect(surface, [255, 255, 255], [0, 0, width, height * 0.15])

    #label:
    lbl_name = guiutil.draw_label(name, width * 0.8, height * 0.15, False, 0.2, False)
    surface.blit(lbl_name, ((width - (width * 0.8)) / 2, height))
    #nr of stones:
    lbl_nr_stones = guiutil.draw_label("# stones: %d" % (nr_black + nr_white), width * 0.8, height * 0.15, False, 0.2, False)
    surface.blit(lbl_nr_stones, ((width - (width * 0.8)) / 2, height + height * 0.15))
    #type:
    lbl_type = guiutil.draw_label(type, width * 0.8, height * 0.15, False, 0.2, False)
    surface.blit(lbl_type, ((width - (width * 0.8)) / 2, height + height * 0.30))
    #score:
    if draw_score:
        lbl_type = guiutil.draw_label("score: %d" % (nr_white - nr_black), width * 0.8, height * 0.15, False, 0.2, False)
        surface.blit(lbl_type, ((width - (width * 0.8)) / 2, height + height * 0.45))

    return surface


def draw_gamestate(game_state, is_revealed):
    """
    Draws the game state on a new surface and returns that surface.
    """
    surface = pygame.Surface((800, 500))
    surface.fill([255, 255, 255])

    pandoras_box = game_state.get_pandoras_box()
    sur_pandoras_box = draw_box("pandora's box", is_revealed, pandoras_box["black"], pandoras_box["white"])
    surface.blit(sur_pandoras_box, (300, 0))

    stone_list = game_state.get_stone_list()
    for i, player in enumerate(game_state.get_player_list()):
        width = 120
        nr_players = len(game_state.get_player_list())

        reveal = is_revealed or player.should_reveal()

        draw_score = game_state.is_finished()
        surface_player = draw_box("player %d" % i, reveal, stone_list[i]["black"], stone_list[i]["white"], player.get_type_name(), draw_score = draw_score)
        surface_player = pygame.transform.scale(surface_player, (width, width * 1.6))
        offset = (800 - (nr_players * width)) / (nr_players + 1)
        surface.blit(surface_player, ((i + 1) * offset + i * width, 270))

    #next turn:
    lbl_turn_count = guiutil.draw_label("next turn:", 220, 30, False, 0.2, False)
    surface.blit(lbl_turn_count, (530, 60))
    lbl_turn_count = guiutil.draw_label(str(game_state.get_next_turn()), 70, 50, False, 0.2, False, font_size = 50)
    surface.blit(lbl_turn_count, (700, 50))
    #next player:
    lbl_turn_count = guiutil.draw_label("next player:", 220, 30, False, 0.2, False)
    surface.blit(lbl_turn_count, (530, 100))
    lbl_turn_count = guiutil.draw_label(str(game_state.get_next_player()), 70, 50, False, 0.2, False, font_size = 50)
    surface.blit(lbl_turn_count, (700, 90))


    #game finished?
    if game_state.is_finished():
        lbl_gameover = guiutil.draw_label("GAME OVER!", 200, 30, False, 0.2, False)
        surface.blit(lbl_gameover, (20, 60))

        highest_score = -1000
        highest_score_idx = -1000
        for i, score in enumerate(game_state.get_final_score_list()):
            if score > highest_score:
                highest_score = score
                highest_score_idx = i
        highest_score_count = 0
        for i, score in enumerate(game_state.get_final_score_list()):
            if score == highest_score:
                highest_score_count += 1
        if highest_score_count > 1:
            winner_string = "winner: tie"
        else:
            winner_string = "winner: player %d" % highest_score_idx

        lbl_gameover = guiutil.draw_label(winner_string, 200, 30, False, 0.2, False)
        surface.blit(lbl_gameover, (20, 100))

    #display last swap:
    last_swap = game_state.get_last_swap()
    if last_swap == None:
        lbl = guiutil.draw_label("last swap; none" , 200, 30, False, 0.2, False, font_size = 20)
    else:
        lbl = guiutil.draw_label("last swap; from: %d, to: %d" % (last_swap["from"], last_swap["to"]), 200, 30, False, 0.2, False, font_size = 20)
    surface.blit(lbl, (20, 140))

    return surface

